import 'package:flutter/material.dart';

///
/// PageView
/// 滑动页面
/// 需要自己实现多页面缓存
/// https://api.flutter-io.cn/flutter/widgets/PageView-class.html
///
///
///
class PageViewPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return PageViewAndTab();
  }
}


class PageViewAndTab extends StatefulWidget {

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return PageViewAndTabState();
  }
}


class PageViewAndTabState extends State<PageViewAndTab>{

  final PageController _pageController = PageController();
  int _selectedIndex = 0;
  
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
   return Scaffold(
        appBar: AppBar(
          title: Text("PageViewAndTab"),
        ),
        body: Center(
          child: PageView(
            controller: _pageController,
            children: [
              Container(
                color: Colors.blue,
                child: Center(
                  child: Text("第一个页面"),
                ),
              ),
              Container(
                color: Colors.yellow,
                child: Center(
                  child: Text("第二个页面"),
                ),
              ),
              Container(
                color: Colors.red,
                child: Center(
                  child: Text("第三个页面"),
                ),
              ),
              Container(
                color: Colors.green,
                child: Center(
                  child: Text("第4个页面"),
                ),
              ),
            ],
            onPageChanged: (index){
              setState(() {
                _selectedIndex = index;
              });
            },
          ),
        ),
     bottomNavigationBar: BottomNavigationBar(
       items: const <BottomNavigationBarItem>[
         BottomNavigationBarItem(
           icon: Icon(Icons.home),
           label: 'Home',
           backgroundColor: Colors.red,
         ),
         BottomNavigationBarItem(
           icon: Icon(Icons.business),
           label: 'Business',
           backgroundColor: Colors.green,
         ),
         BottomNavigationBarItem(
           icon: Icon(Icons.school),
           label: 'School',
           backgroundColor: Colors.blue,
         ),
         BottomNavigationBarItem(
           icon: Icon(Icons.settings),
           backgroundColor: Colors.deepPurple,
           label: 'Settings',
         ),
       ],
       currentIndex: _selectedIndex,
       selectedItemColor: Colors.amber[800],
       onTap: (int index){
         setState(() {
           _selectedIndex = index;
           _pageController.jumpToPage(_selectedIndex);
         });
       },
     ),
    );
  }


  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    _pageController.dispose();
    super.dispose();
  }

}
